package com.thingcom.delinrobot.main.adddevice;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.thingcom.delinrobot.Common.Constant;
import com.thingcom.delinrobot.Common.pjo.AddDeviceTypeItem;
import com.thingcom.delinrobot.R;
import com.thingcom.delinrobot.base.BaseBackFragment;
import com.thingcom.delinrobot.utils.PreferencesUtil;
import com.thingcom.delinrobot.utils.SpacesItemDecoration;
import com.thingcom.delinrobot.view.SimpleToolbar;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;

import static com.thingcom.delinrobot.utils.AppUtils.hideSoftKeyboard;

/**
 * author : YuFeng
 * e-mail : 923462992@qq.com
 * date   : 2019/12/10
 */
public class AddDeviceZeroFragment extends BaseBackFragment {

    @BindView(R.id.toolbar_add_device_zero)
    SimpleToolbar toolbarAddDeviceZero;
    @BindView(R.id.recycler_device_type_choose)
    RecyclerView recyclerDeviceTypeChoose;


    public static AddDeviceZeroFragment newInstance() {
        Bundle args = new Bundle();
        AddDeviceZeroFragment fragment = new AddDeviceZeroFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_back_device_add_zero, container, false);
        unbinder = ButterKnife.bind(this, view);
        initToolbar1(toolbarAddDeviceZero);
        initView();
        return view;
    }

    private void initToolbar1(SimpleToolbar toolbar){
        toolbar.setMyLeftIcon(R.drawable.toolbar_back_two_selector);
        toolbar.setMinimumHeight(42);
        toolbar.setBackgroundColor(getResources().getColor(R.color.black_main));
        toolbar.setTitleStyle(getResources().getColor(R.color.white_main),13);
        toolbar.setLeftIconClickListener(view -> {
            hideSoftKeyboard(_mActivity);
            Intent intent = new  Intent(getContext(),DeviceListActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        });

    }
    private void initView(){
        List<AddDeviceTypeItem> list = new ArrayList<>();
        list.add(new AddDeviceTypeItem(R.drawable.device_type_18,""));
        list.add(new AddDeviceTypeItem(R.drawable.device_type_24,""));
//        list.add(new AddDeviceTypeItem(R.drawable.device_type_one,""));
        AddDeviceTypeAdapter adapter = new AddDeviceTypeAdapter(list);
        recyclerDeviceTypeChoose.setLayoutManager(new LinearLayoutManager(getContext(),RecyclerView.VERTICAL,false));
        recyclerDeviceTypeChoose.addItemDecoration(new SpacesItemDecoration(12));
        adapter.bindToRecyclerView(recyclerDeviceTypeChoose);
        adapter.setOnItemClickListener((adapter1, view, position) -> {
            PreferencesUtil.getInstance(getContext()).putInt(Constant.ROBOT_CHOOSE,position);
            start(AddDeviceFirstFragment.newInstance());
        });
    }

    private class AddDeviceTypeAdapter extends BaseQuickAdapter<AddDeviceTypeItem, BaseViewHolder> {


        AddDeviceTypeAdapter(@Nullable List<AddDeviceTypeItem> data) {
            super(R.layout.item_add_device_type,data);
        }

        @Override
        protected void convert(BaseViewHolder helper, AddDeviceTypeItem item) {
            helper.setText(R.id.tv_add_device_name, item.getName());
            ImageView imageView = helper.getView(R.id.iv_add_device_type);
            imageView.setImageResource(item.getDeviceImage());


        }
    }

    @Override
    public boolean onBackPressedSupport() {
        Intent intent = new  Intent(getContext(),DeviceListActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        return super.onBackPressedSupport();
    }
}
